package com.zhang.server.controller;

import com.zhang.server.common.Result;
import com.zhang.server.controller.request.BookPageRequest;
import com.zhang.server.entity.Book;
import com.zhang.server.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    IBookService bookService;

    @GetMapping("/list")
    public Result list(){
        return Result.success(bookService.list());
    }

    @GetMapping("/page")
    public Result page(BookPageRequest bookPageRequest){
        if(bookPageRequest.getPageNum() == null){
            bookPageRequest.setPageNum(1);
        }

        if(bookPageRequest.getPageSize() == null){
            bookPageRequest.setPageSize(10);
        }
        return Result.success(bookService.page(bookPageRequest));
    }

    @PostMapping("/save")
    public Result save(@RequestBody Book book){
        bookService.save(book);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return bookService.getById(id);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        bookService.delete(id);
        return Result.success();
    }

}
